package com.eqgis.supermap;

import android.annotation.SuppressLint;
import android.os.AsyncTask;

import com.supermap.data.Datasources;
import com.supermap.data.Workspace;
import com.supermap.data.WorkspaceConnectionInfo;
import com.supermap.data.WorkspaceType;

import java.util.ArrayList;
import java.util.List;

/**
 * 简易工具：
 * 地图数据管理器
 * <p>
 *     用于打开工作空间以及管理地图数据
 * </p>
 * @author tanyx 2022/4/7
 * @version 1.0
 * <br/>SampleCode:<br/>
 * <code>
 *
 * </code>
 **/
public class MapDataManager {
    private static MapDataManager dataManager = null;

    private LoadDataCallback loadDataCallback;
    private List<MapData> mapDataList;

    public class MapData{
        private int index;
        private Workspace workspace;
        private Datasources datasources;

        /**
         * 获取索引
         * @return 索引
         */
        public int getIndex() {
            return index;
        }

        /**
         * 获取工作空间
         * @return 工作空间
         */
        public Workspace getWorkspace() {
            return workspace;
        }

        /**
         * 获取数据源集合
         * @return 数据源集合
         */
        public Datasources getDatasourceSet() {
            return datasources;
        }
    }

    public List<MapData> getMapDataList() {
        return mapDataList;
    }

    /**
     * 构造函数
     */
    private MapDataManager() {
        mapDataList = new ArrayList<>();
    }

    /**
     * 加载数据
     * @return SMWU类型的工作空间路径
     */
    @SuppressLint("StaticFieldLeak")
    public MapDataManager loadData(String... path){
        new AsyncTask<String, MapData, MapDataManager>() {
            @Override
            protected void onProgressUpdate(MapData... values) {
//                pgbar.setProgress(value);
                MapData value = values[0];
                loadComplete(value);
            }

            @Override
            protected MapDataManager doInBackground(String... strings) {
                for (int i = 0; i < strings.length; i++) {
                    //读取数据
                    Workspace workspace = new Workspace();
                    WorkspaceConnectionInfo workspaceConnectionInfo = new WorkspaceConnectionInfo();
                    workspaceConnectionInfo.setType(WorkspaceType.SMWU);
                    workspaceConnectionInfo.setServer(strings[i]);
                    boolean open = workspace.open(workspaceConnectionInfo);

                    MapData mapData = new MapData();
                    mapData.index = i;
                    mapData.workspace = workspace;

                    if (open){
                        Datasources datasources = workspace.getDatasources();
                        if (datasources == null){
                            onFailed(i,"无数据源",new NullPointerException("第"+ i +"个工作空间，无数据源"));
                        }
                        mapData.datasources = datasources;
                    }else {
                        onFailed(i,"工作空间打开失败！",new RuntimeException("工作空间打开失败！"));
                    }

                    mapDataList.add(mapData);
                    publishProgress(mapData);
                }
                return getInstance();
            }

            @Override
            protected void onPostExecute(MapDataManager s) {
                onSuccess();//成功回调
            }
        }.execute(path);

        return this;
    }

    /**
     * 关闭并销毁
     */
    public void dispose(){
        for (MapData mapData : dataManager.mapDataList) {
            Datasources datasources = mapData.getDatasourceSet();
            Workspace workspace = mapData.getWorkspace();
            if (datasources!=null){
                datasources.closeAll();
            }
            if (workspace!=null){
                workspace.close();
                workspace.dispose();
            }
        }
    }

    /**
     * 获取实例（单例）
     * @return 实例
     */
    public static MapDataManager getInstance(){
        if (dataManager == null){
            synchronized (MapDataManager.class){
                if (dataManager == null){
                    dataManager = new MapDataManager();
                }
            }
        }
        return dataManager;
    }

    /**
     * 设置数据加载监听回调
     * @param loadDataCallback 数据加载状态回调
     */
    public MapDataManager setLoadDataCallback(LoadDataCallback loadDataCallback) {
        this.loadDataCallback = loadDataCallback;
        return this;
    }

    /**
     * 数据加载成功
     */
    private void onSuccess(){
        if (loadDataCallback!=null)
            loadDataCallback.onSuccess();
    }

    /**
     * 数据加载失败
     */
    private void onFailed(int index,String msg,Exception e){
        if (loadDataCallback!=null){
            loadDataCallback.onFailed(index,msg,e);
        }
    }

    /**
     * 读取完成
     * @param mapData 所读取的数据
     */
    private void loadComplete(MapData mapData){
        if (loadDataCallback!=null){
            loadDataCallback.loadComplete(mapData);
        }
    }

}
